//设计器----矩形选框

//选择框核心代码
function resizeToPoint(x,y){
	console.log(x,y);
    var width = Math.abs(x-startX);
    var height = Math.abs(y-startY);
    rector.width(width);
    rector.height(height);
    //当鼠标x方向上为向左拖动 设置left位置
    if(x<startX){
        rector.css({
            left:(startX-width)+'px',
        });
    }
    //当鼠标y方向上为向上拖动 设置top位置
    if(y<startY){
        rector.css({
            top:(startY-height)+'px',
        });
    }
}

/**
 *核心相交算法
 * @param rect1{x1,y1,x2,y2}
 * @param rect2 {x1,y1,x2,y2}
 */
function isCross(rect1,rect2){
    var xNotCross=true;//x方向上不重合
    var yNotCross = true;//y方向上不重合
    xNotCross =((rect1.x1>rect2.x2) || (rect2.x1>rect1.x2));
    yNotCross =  ((rect1.y1>rect2.y2) || (rect2.y1>rect1.y2));
    return !(xNotCross || yNotCross);
}
/**
 *获取元素的矩形的起始点坐标与其对角点坐标
 * @param $el
 * @return {x1,y1,x2,y2}
 */
function getRect($el){
    var x1 = $el.offset().left;
    var y1 = $el.offset().top;
    var x2 = x1 +$el.outerWidth();
    var y2 = y1+$el.outerHeight();
    return {x1,x2,y1,y2};
}

//框选处理 如果元素与选择框相交则设置样式
function handleRectSelection(){
    var selectionReact = getRect(rector);
    $('.dragbox').each(function(){
        var rect = getRect($(this));
        if(isCross(selectionReact,rect)){
            $(this).addClass('selected');
        }else{
            $(this).removeClass('selected');
        }
    });
}
